package com.xszx.controller;

import com.github.pagehelper.PageInfo;
import com.xszx.config.PageRequest;
import com.xszx.entity.GoodInOutInfo;
import com.xszx.service.InOutService;
import com.xszx.vo.AjaxJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/inOut")
public class InOutController {
    @Autowired
    private InOutService inOutService;

    //获得入库记录表
    @RequestMapping("getInWarehouseRecord")
    private AjaxJson getInWarehouseRecord(PageRequest pageRequest,
                                          @RequestParam(defaultValue = "1") Integer page,
                                          @RequestParam(defaultValue = "10") Integer size){
        pageRequest.setPage(page);
        pageRequest.setSize(size);
        AjaxJson ajaxJson = new AjaxJson();
        PageInfo<GoodInOutInfo> list = inOutService.getInWarehouseRecord(pageRequest);
        ajaxJson.put("list",list);
        return ajaxJson;
    }
    //获得出库记录表
    @RequestMapping("/getOutWarehouseRecord")
    private AjaxJson getOutWarehouseRecord(PageRequest pageRequest,
                                           @RequestParam(defaultValue = "1") Integer page,
                                           @RequestParam(defaultValue = "10") Integer size){
        pageRequest.setPage(page);
        pageRequest.setSize(size);
        AjaxJson ajaxJson = new AjaxJson();
        PageInfo<GoodInOutInfo> list = inOutService.getOutWarehouseRecord(pageRequest);
        ajaxJson.put("list",list);
        return ajaxJson;
    }

    @PostMapping("/exportOutWarehouseRecord")
    public void exportOutWarehouseRecord(
           @RequestBody(required = false) Map<String, Object> params,
            HttpServletResponse response) throws IOException {
        List<Integer> ids = (List<Integer>) params.get("ids");
        String wnumber = (String) params.get("wnumber");
        String rcode = (String) params.get("rcode");
        String rlocation = (String) params.get("rlocation");
        String gtype = (String) params.get("gtype");
        String gbatch = (String) params.get("gbatch");
        String time = (String) params.get("time");
        Boolean exportAll = (Boolean) params.get("exportAll");
        System.out.println("ids:"+ids);
        // 设置响应头
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("UTF-8");
        String fileName = URLEncoder.encode("出库记录导出", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + fileName + ".xlsx");

        try (OutputStream os = response.getOutputStream()) {
            // 调用服务层导出方法
            inOutService.exportOutWarehouseRecord(ids, wnumber, rcode, rlocation, gtype, gbatch, time, exportAll, os);
        } catch (Exception e) {
            e.printStackTrace();
            // 处理导出异常（可根据需要返回错误信息）
            response.setStatus(500);
            response.getWriter().write("导出失败：" + e.getMessage());
        }
    }
    @PostMapping("/exportIntWarehouseRecord")
    public void exportIntWarehouseRecord(
            @RequestBody(required = false) Map<String, Object> params,
            HttpServletResponse response) throws IOException {
        List<Integer> ids = (List<Integer>) params.get("ids");
        String wnumber = (String) params.get("wnumber");
        String rcode = (String) params.get("rcode");
        String rlocation = (String) params.get("rlocation");
        String gtype = (String) params.get("gtype");
        String gbatch = (String) params.get("gbatch");
        String time = (String) params.get("time");
        Boolean exportAll = (Boolean) params.get("exportAll");
        System.out.println("ids:"+ids);
        // 设置响应头
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("UTF-8");
        String fileName = URLEncoder.encode("入库记录导出", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + fileName + ".xlsx");

        try(OutputStream os = response.getOutputStream()){
            //调用服务层导出方法
            inOutService.exportIntWarehouseRecord(ids, wnumber, rcode, rlocation, gtype, gbatch, time, exportAll,os);
        }catch (Exception e) {
            e.printStackTrace();
            // 处理导出异常（可根据需要返回错误信息）
            response.setStatus(500);
            response.getWriter().write("导出失败：" + e.getMessage());
        }
    }



    //库存总量  及变化趋势
    @RequestMapping("/getAlertItems")
    private AjaxJson getAlertItems(){
        AjaxJson j = new AjaxJson();
        double AlertItems =  inOutService.getAlertItems();
        double AlertTrend = inOutService.getAlertTrend();
        j.put("alertItems",AlertItems);
        j.put("alertTrend",AlertTrend);
        return j;
    }
}
